package com.onenine.app.view;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.onenine.app.R;

import cn.jzvd.JZMediaManager;
import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;

/**
 * 小屏无声循环播放
 */
public class JzvdStdVolumeLoopAfterFullscreen extends JzvdStd {

    public JzvdStdVolumeLoopAfterFullscreen(Context context) {
        super(context);
    }

    public JzvdStdVolumeLoopAfterFullscreen(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void onPrepared() {
        super.onPrepared();
        AUTO_COMPLETION_QUIT_FULLSCREEN = false;
        SHOW_BATTERY_INFO = false;
        setScreenWidgetVisible(currentScreen == SCREEN_WINDOW_FULLSCREEN);
        if (currentScreen == SCREEN_WINDOW_FULLSCREEN) {
            setVideoImageDisplayType(Jzvd.VIDEO_IMAGE_DISPLAY_TYPE_FILL_SCROP);
            JZMediaManager.instance().jzMediaInterface.setVolume(1f, 1f);
        } else {
            setVideoImageDisplayType(Jzvd.VIDEO_IMAGE_DISPLAY_TYPE_ADAPTER);
            JZMediaManager.instance().jzMediaInterface.setVolume(0f, 0f);
        }
    }

    /**
     * 进入全屏模式的时候关闭静音模式
     */
    @Override
    public void startWindowFullscreen() {
        setVideoImageDisplayType(Jzvd.VIDEO_IMAGE_DISPLAY_TYPE_ADAPTER);
        super.startWindowFullscreen();
        JZMediaManager.instance().jzMediaInterface.setVolume(1f, 1f);
    }

    /**
     * 退出全屏模式的时候开启静音模式
     */
    @Override
    public void playOnThisJzvd() {
        setVideoImageDisplayType(Jzvd.VIDEO_IMAGE_DISPLAY_TYPE_FILL_SCROP);
        super.playOnThisJzvd();
        JZMediaManager.instance().jzMediaInterface.setVolume(0f, 0f);
    }

    /**
     * 设置全屏或小屏的控件可见性
     *
     * @param visible
     */
    private void setScreenWidgetVisible(boolean visible) {
        FULLSCREEN_ORIENTATION = ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT;
        fullscreenButton.setVisibility(visible ? View.VISIBLE : View.GONE);
        currentTimeTextView.setVisibility(visible ? View.VISIBLE : View.GONE);
        totalTimeTextView.setVisibility(visible ? View.VISIBLE : View.GONE);
        progressBar.setVisibility(visible ? View.VISIBLE : View.GONE);
        bottomProgressBar.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    /**
     * 小屏点击进入大屏模式
     *
     * @param v
     * @param event
     * @return
     */
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        boolean ret = super.onTouch(v, event);
        if (event.getAction() == MotionEvent.ACTION_UP && v.getId() == R.id.surface_container
                && !mChangePosition && !mChangeVolume && currentScreen != SCREEN_WINDOW_FULLSCREEN) {
            startWindowFullscreen();
        }
        return ret;
    }

    /**
     * 播放时小屏不显示进度条
     */
    public void changeUiToPlayingClear() {
        switch (currentScreen) {
            case SCREEN_WINDOW_NORMAL:
            case SCREEN_WINDOW_LIST:
                setAllControlsVisiblity(View.INVISIBLE, View.INVISIBLE, View.INVISIBLE,
                        View.INVISIBLE, View.INVISIBLE, View.INVISIBLE, View.INVISIBLE);
                break;
            case SCREEN_WINDOW_FULLSCREEN:
                setAllControlsVisiblity(View.INVISIBLE, View.INVISIBLE, View.INVISIBLE,
                        View.INVISIBLE, View.INVISIBLE, View.VISIBLE, View.INVISIBLE);
                break;
            case SCREEN_WINDOW_TINY:
                break;
        }

    }

}
